<template>
	<div>comment</div>
</template>
<script>
import userList from "../../assets/users.json";
import talkList from "../../assets/talks.json";
export default {
	name: "comment-view",
	props: {
		view: {
			type: Object,
			default: () => ({})
		}
	},
	data() {
		return {
			list: []
		};
	},
	computed: {
		options() {
			return this.view.options || {};
		}
	},
	watch: {
		"options.limit"() {
			this.getCommentList();
		}
	},
	created() {
		this.getCommentList();
	},
	methods: {
		getCommentList() {
			const { limit } = this.options;
			let now = new Date().getTime();
			let list = [];
			let usrLen = userList.length;
			let talkLen = talkList.length;
			for (let i = 0; i < limit; i++) {
				now -= Math.ceil(Math.random() * 1000 * Math.ceil(Math.random() * 80));
				list.push({
					avatar: this.randomNum(usrLen) + ".jpeg",
					rate: 5,
					user_name: userList[this.randomNum(usrLen)] || "未知",
					content: talkList[this.randomNum(talkLen)] || "非常的赞！",
					time: this.timeAgo(now)
				});
			}
			this.list = list;
		},
		randomNum(max) {
			return Math.ceil(Math.random() * max);
		},
		//timestamp是一个时间毫秒，注意时间戳是秒的形式，在这个毫秒的基础上除以1000，就是十位数的时间戳。13位数的都是时间毫秒。
		timeAgo(timestamp) {
			var mistiming = Math.round((Date.now() - timestamp) / 1000);
			var arrr = ["年", "个月", "星期", "天", "小时", "分钟", "秒"];
			var arrn = [31536000, 2592000, 604800, 86400, 3600, 60, 1];
			for (var i = 0; i < arrn.length; i++) {
				var inm = Math.floor(mistiming / arrn[i]);
				if (inm != 0) {
					return inm + arrr[i] + "前";
				}
			}
		}
	}
};
</script>
